package com.xyauto.theme.bean;

import android.os.Parcel;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xyauto.theme.App;
import com.xyauto.theme.BuildConfig;
import com.xyauto.theme.Constant;
import com.xyauto.theme.utils.SelectManager;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class NetworkThemeBean implements IThemeBean {

    private static final String TAG = "NetworkThemeBean";
    /**
     * {
     * "id": "1",
     * "type": "2",
     * "platform": "92平台",
     * "customer": "顺信达",
     * "title": "133",
     * "url": "http://store.xygala.com/data/uitheme/202303/THD_2021-04-03170446003000.zip",
     * "image": "http://store.xygala.com/data/uitheme/202303/户型图.jpg",
     * "remark": "66",
     * "add_time": "1677947689",
     * "last_time": "1679044580",
     * "status": "1"
     * }
     */


    private String id;
    private String title;
    private String url;
    private String image;
    private String ver;
    private ArrayList<String> theme_images;


    /**
     * 本地内容，动态获取
     */
    private String apkPath;
    private String previewPath;
    private ArrayList<String> previewsPath;

    public static final Creator<NetworkThemeBean> CREATOR = new Creator<NetworkThemeBean>() {
        @Override
        public NetworkThemeBean createFromParcel(Parcel in) {
            return new NetworkThemeBean(in);
        }

        @Override
        public NetworkThemeBean[] newArray(int size) {
            return new NetworkThemeBean[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    protected NetworkThemeBean(Parcel in) {
        id = in.readString();
        title = in.readString();
        url = in.readString();
        image = in.readString();
        ver = in.readString();
        theme_images = in.createStringArrayList();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(title);
        dest.writeString(url);
        dest.writeString(image);
        dest.writeString(ver);
        dest.writeStringList(theme_images);
    }

    @Override
    public boolean isSelected() {
        return SelectManager.INSTANCE.isSelectTheme(title);
    }

    @NonNull
    @Override
    public String getName() {
        return title;
    }

    @Nullable
    @Override
    public String getUrlPath() {
        return url;
    }

    @NonNull
    @Override
    public String getVersion() {
        return App.Companion.findLauncherVer();
    }

    @NonNull
    @Override
    public String getPreviewImage() {
        if (isExists()) {
            return previewPath;
        }
        return image;
    }


    @Nullable
    @Override
    public String getApkPath() {
        redirectLocalFiles();
        return apkPath;
    }


    @Nullable
    @Override
    public List<String> getPreviewImages() {
        if (isExists()) {
            return previewsPath;
        } else if (theme_images != null && theme_images.size() > 0) {
            return theme_images;
        }
        return null;
    }

    @Override
    public boolean isExists() {
        redirectLocalFiles();
        return getLocalType() > 0;
    }

    /**
     * 本地类型
     * 0=未重定向
     * 1=预置类型
     * 2=下载文件
     * -1=未找到文件
     */
    private int localType = 0;

    public int getLocalType() {
        return localType;
    }

    private void redirectLocalFiles() {
        if (localType == 0 && redirectLocalFiles(BuildConfig.DEFAULT_THEME_PATH)) {
            localType = 1;
        }
        if (localType == 0 && redirectLocalFiles(Constant.INSTANCE.getSavePath(getVersion()))) {
            localType = 2;
        }
        if (localType == 0) {
            localType = -1;
        }
    }


    private boolean redirectLocalFiles(String rootDir) {
        File file = new File(rootDir, title);
        if (!file.exists() || !file.isDirectory()) return false;
        File[] listFiles = file.listFiles();
        if (listFiles == null || listFiles.length <= 0) return false;
        previewsPath = new ArrayList<>();
        previewPath = null;
        apkPath = null;
        for (File itemFile : listFiles) {
            String fileName = itemFile.getName();
            if (itemFile.isDirectory()) continue;
            if (fileName.endsWith("apk")) {
                apkPath = itemFile.getAbsolutePath();
                continue;
            }
            if (fileName.endsWith("webp") || fileName.endsWith("png") || fileName.endsWith("jpg") ||
                    fileName.endsWith("jpeg") || fileName.endsWith("gif") || fileName.endsWith("bmp")) {
                previewsPath.add(itemFile.getAbsolutePath());
            }
        }
        if (previewsPath.size() > 0) {
            previewPath = previewsPath.get(0);
        }
        return !TextUtils.isEmpty(apkPath) && previewsPath.size() > 0;
    }
}
